package org.jeecg.modules.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONTokener;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.device.Device;
import org.jeecg.modules.entity.TigDevice;
import org.jeecg.modules.service.DeviceService;
import org.jeecg.modules.service.ITigDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 设备通信
 * @Author: jianxing
 * @Date:   2022-03-07
 * @Version: V1.0
 */
@Api(tags="设备")
@RestController
@RequestMapping("/tiger/tigDeviceCommu")
@Slf4j
public class TigDeviceCommuController extends JeecgController<TigDevice, ITigDeviceService>  {

    @Autowired
    DeviceService deviceService;

    @Autowired
    RedisUtil redisUtil;


    /**
     *  连接设备
     * @param deviceId
     * @return
     */
    @AutoLog(value = "设备-连接")
    @ApiOperation(value="设备-连接", notes="设备-连接")
    @GetMapping(value = "/connect")
    public Result<String> connect(@RequestParam String deviceId,@RequestParam(name = "manualConnect", defaultValue = "false") Boolean manualConnect) {
        if(deviceService.login(deviceId,manualConnect)){

        }else{
            Result.error("连接失败！设备未准备好");
        }
        return Result.OK("已发送登录请求");
    }

    /**
     *  连接设备
     * @param deviceId
     * @return
     */
    @AutoLog(value = "设备-断开")
    @ApiOperation(value="设备-断开", notes="设备-断开")
    @GetMapping(value = "/disconnect")
    public Result<String> disconnect(@RequestParam String deviceId,@RequestParam(name = "manualDisconnect", defaultValue = "false") Boolean manualDisconnect) {
        deviceService.disconnect(deviceId,manualDisconnect);
        return Result.OK("已发送断开请求");
    }

    @AutoLog(value = "设备-获取")
    @ApiOperation(value="设备-获取", notes="设备-获取")
    @GetMapping(value = "/getDeviceinfo")
    public Result<String> getDeviceinfo(@RequestParam String deviceId){
        try {
            Device d = deviceService.getDevice(deviceId);
            TigDevice tigd = deviceService.getTigDevice(deviceId);
            String strDeviceJson = JSON.toJSONString(tigd);
            com.alibaba.fastjson.JSONObject deviceJson = com.alibaba.fastjson.JSONObject.parseObject(strDeviceJson);
            com.alibaba.fastjson.JSONObject shadowJson = com.alibaba.fastjson.JSONObject.parseObject(d.getShadow().toString());
            deviceJson.put("shadow",shadowJson);
            return Result.OK(deviceJson.toString());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("没有该设备"+deviceId);
        }

    }

    /**
     *  连接设备
     * @param cmd,data
     * @return
     */
    @AutoLog(value = "设备-指令")
    @ApiOperation(value="设备-指令", notes="设备-指令")
    @GetMapping(value = "/setDevice")
    public Result<String> setDevice(@RequestParam String cmd,@RequestParam String data) {
        try{
            JSONObject json = new JSONObject(new JSONTokener(data));
            if(deviceService.runCommand(cmd,json)){

            }else{
                Result.error("设备不支持:"+cmd);
            }
        }catch (Exception e){
            e.printStackTrace();
            Result.error("设备内容不符");
        }

        return Result.OK("已发送请求");
    }
}
